package vertx;

import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.datagram.DatagramSocket;
import io.vertx.core.datagram.DatagramSocketOptions;
import test.PerformanceAnalyze;

import java.util.concurrent.CountDownLatch;

public class DatagramSocketServer {

    public void start() throws InterruptedException {
        PerformanceAnalyze performanceAnalyze = new PerformanceAnalyze();

        String host = System.getProperty("publicAddress", "127.0.0.1");

        Vertx vertx = Vertx.vertx();
        DatagramSocket socket = vertx.createDatagramSocket(new DatagramSocketOptions());
        DatagramSocket client = vertx.createDatagramSocket(new DatagramSocketOptions());

        socket.listen(8899, "0.0.0.0", asyncResult -> {
            if (asyncResult.succeeded()) {
                socket.handler(packet -> {
                    client.send(Buffer.buffer(packet.data().toString()), 9988, host);

                    performanceAnalyze.meter();

                });
            } else {
                System.out.println("Listen failed" + asyncResult.cause());
            }
        });

        new CountDownLatch(1).await();
    }


    public static void main(String[] args) {

    }
}
